package base.service.dao;

import com.ibatis.sqlmap.client.event.RowHandler;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <ol>
 * date:11-4-11 editor:yanghongjian
 * <li>创建文档</li>
 * </ol>
 * <ol>
 *
 * @author <a href="mailto:12719889@qq.com">YangHongJian</a>
 * @version 2.0
 * @since 1.4.2
 */
public class MapRowHandler implements RowHandler {
    private Map map;
    private List list = new ArrayList();

    public void handleRow(Object o) {
        map = (Map) o;
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            Object key = entry.getKey();
            Object val = entry.getValue();

            if (val != null) {
                if (val instanceof Timestamp) //定义格式并转化成字符型日期格式
                    val = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Timestamp(((Timestamp) val).getTime()));
                if (val instanceof Date) //定义格式并转化成字符型日期格式
                    val = (new SimpleDateFormat("yyyy-MM-dd")).format(val);
                if(val instanceof BigDecimal)
                    val = val.toString();
            }
            map.put(key, val != null ? val : "");
        }
        list.add(map);
    }

    /**
     * @deprecated
     */
    public void handleRow(Object o, List list) {

    }

    public Map getMap() {
        return map;
    }

    public List getMapList() {
        return list;
    }
}
